﻿// Copyright (C) 2009 - 2013 EvTech Project
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Piirto
{
    public partial class InformationDialog : Form
    {
        protected List<Button> controlButtons = new List<Button>();

        public InformationDialog(String labelText, MessageBoxButtons buttonFlags = MessageBoxButtons.OK)
        {
            InitializeComponent();
            TableLayoutPanel tableLayoutPanelButtons = new TableLayoutPanel();
            this.tableLayoutPanel1.Controls.Add(tableLayoutPanelButtons, 1, 3);
            tableLayoutPanelButtons.Dock = DockStyle.Fill;
            tableLayoutPanelButtons.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;
            tableLayoutPanelButtons.RowCount = 1;
            tableLayoutPanelButtons.RowStyles.Add(new RowStyle(SizeType.Percent, 100F));
            switch (buttonFlags)
            {
                case MessageBoxButtons.YesNo:
                    {
                        this.Text = "Vahvistus";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 3;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        Button btnYes = new Button();
                        btnYes.DialogResult = DialogResult.Yes;
                        tableLayoutPanelButtons.Controls.Add(btnYes, 0, 0);
                        btnYes.Font = new Font("Comic Sans MS", 40f);
                        btnYes.Text = "Kyllä";
                        btnYes.UseVisualStyleBackColor = true;
                        btnYes.Dock = DockStyle.Fill;
                        controlButtons.Add(btnYes);

                        Button btnNo = new Button();
                        btnNo.DialogResult = DialogResult.No;
                        tableLayoutPanelButtons.Controls.Add(btnNo, 2, 0);
                        btnNo.Font = new Font("Comic Sans MS", 20f);
                        btnNo.Text = "Ei";
                        btnNo.UseVisualStyleBackColor = true;
                        btnNo.Dock = DockStyle.Fill;
                        controlButtons.Add(btnNo);
                    }
                    break;
                case MessageBoxButtons.OK:
                    {
                        this.Text = "Ilmoitus";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 3;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        Button btnOk = new Button();
                        btnOk.DialogResult = DialogResult.OK;
                        tableLayoutPanelButtons.Controls.Add(btnOk, 1, 0);
                        btnOk.Font = new Font("Comic Sans MS", 20f);
                        btnOk.Text = "Ok";
                        btnOk.UseVisualStyleBackColor = true;
                        btnOk.Dock = DockStyle.Fill;
                        controlButtons.Add(btnOk);
                    }
                    break;
                case MessageBoxButtons.OKCancel:
                    {
                        this.Text = "Vahvistus";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 3;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        Button btnOk = new Button();
                        btnOk.DialogResult = DialogResult.OK;
                        tableLayoutPanelButtons.Controls.Add(btnOk, 0, 0);
                        btnOk.Font = new Font("Comic Sans MS", 20f);
                        btnOk.Text = "Hyväksy";
                        btnOk.UseVisualStyleBackColor = true;
                        btnOk.Dock = DockStyle.Fill;
                        controlButtons.Add(btnOk);

                        Button btnCancel = new Button();
                        btnCancel.DialogResult = DialogResult.Cancel;
                        tableLayoutPanelButtons.Controls.Add(btnCancel, 2, 0);
                        btnCancel.Font = new Font("Comic Sans MS", 20f);
                        btnCancel.Text = "Peru";
                        btnCancel.UseVisualStyleBackColor = true;
                        btnCancel.Dock = DockStyle.Fill;
                        controlButtons.Add(btnCancel);
                    }
                    break;
                case MessageBoxButtons.RetryCancel:
                    {
                        this.Text = "Epäonnistui";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 3;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        Button btnRetry = new Button();
                        btnRetry.DialogResult = DialogResult.Retry;
                        tableLayoutPanelButtons.Controls.Add(btnRetry, 0, 0);
                        btnRetry.Font = new Font("Comic Sans MS", 20f);
                        btnRetry.Text = "Ok";
                        btnRetry.UseVisualStyleBackColor = true;
                        btnRetry.Dock = DockStyle.Fill;
                        controlButtons.Add(btnRetry);

                        Button btnCancel = new Button();
                        btnCancel.DialogResult = DialogResult.Cancel;
                        tableLayoutPanelButtons.Controls.Add(btnCancel, 2, 0);
                        btnCancel.Font = new Font("Comic Sans MS", 20f);
                        btnCancel.Text = "Peru";
                        btnCancel.UseVisualStyleBackColor = true;
                        btnCancel.Dock = DockStyle.Fill;
                        controlButtons.Add(btnCancel);
                    }
                    break;
                case MessageBoxButtons.YesNoCancel:
                    {
                        this.Text = "Vahvistus";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 5;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 5F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 5F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
                        Button btnYes = new Button();
                        btnYes.DialogResult = DialogResult.Yes;
                        tableLayoutPanelButtons.Controls.Add(btnYes, 0, 0);
                        btnYes.Font = new Font("Comic Sans MS", 20f);
                        btnYes.Text = "Kyllä";
                        btnYes.UseVisualStyleBackColor = true;
                        btnYes.Dock = DockStyle.Fill;
                        controlButtons.Add(btnYes);

                        Button btnNo = new Button();
                        btnNo.DialogResult = DialogResult.No;
                        tableLayoutPanelButtons.Controls.Add(btnNo, 2, 0);
                        btnNo.Font = new Font("Comic Sans MS", 20f);
                        btnNo.Text = "Ei";
                        btnNo.UseVisualStyleBackColor = true;
                        btnNo.Dock = DockStyle.Fill;
                        controlButtons.Add(btnNo);

                        Button btnCancel = new Button();
                        btnCancel.DialogResult = DialogResult.Cancel;
                        tableLayoutPanelButtons.Controls.Add(btnCancel, 4, 0);
                        btnCancel.Font = new Font("Comic Sans MS", 20f);
                        btnCancel.Text = "Peru";
                        btnCancel.UseVisualStyleBackColor = true;
                        btnCancel.Dock = DockStyle.Fill;
                        controlButtons.Add(btnCancel);
                    }
                    break;
                case MessageBoxButtons.AbortRetryIgnore:
                    {
                        this.Text = "Vahvistus";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 5;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 5F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 5F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
                        Button btnAbort = new Button();
                        btnAbort.DialogResult = DialogResult.Abort;
                        tableLayoutPanelButtons.Controls.Add(btnAbort, 0, 0);
                        btnAbort.Font = new Font("Comic Sans MS", 20f);
                        btnAbort.Text = "Peru";
                        btnAbort.UseVisualStyleBackColor = true;
                        btnAbort.Dock = DockStyle.Fill;
                        controlButtons.Add(btnAbort);

                        Button btnRetry = new Button();
                        btnRetry.DialogResult = DialogResult.Retry;
                        tableLayoutPanelButtons.Controls.Add(btnRetry, 2, 0);
                        btnRetry.Font = new Font("Comic Sans MS", 20f);
                        btnRetry.Text = "Uudelleen";
                        btnRetry.UseVisualStyleBackColor = true;
                        btnRetry.Dock = DockStyle.Fill;
                        controlButtons.Add(btnRetry);

                        Button btnIgnore = new Button();
                        btnIgnore.DialogResult = DialogResult.Ignore;
                        tableLayoutPanelButtons.Controls.Add(btnIgnore, 4, 0);
                        btnIgnore.Font = new Font("Comic Sans MS", 20f);
                        btnIgnore.Text = "Ohita";
                        btnIgnore.UseVisualStyleBackColor = true;
                        btnIgnore.Dock = DockStyle.Fill;
                        controlButtons.Add(btnIgnore);
                    }
                    break;
                default:
                    {
                        this.Text = "Ilmoitus";
                        this.label1.Text = labelText;
                        tableLayoutPanelButtons.ColumnCount = 3;
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        tableLayoutPanelButtons.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33F));
                        Button btnOk = new Button();
                        btnOk.DialogResult = DialogResult.OK;
                        tableLayoutPanelButtons.Controls.Add(btnOk, 1, 0);
                        btnOk.Font = new Font("Comic Sans MS", 20f);
                        btnOk.Text = "Ok";
                        btnOk.UseVisualStyleBackColor = true;
                        btnOk.Dock = DockStyle.Fill;
                        controlButtons.Add(btnOk);
                    }
                    break;
            }
            setComponents();
        }

        private void InformationDialog_Resize(object sender, EventArgs e)
        {
            setComponents();
        }

        private void setComponents()
        {
            foreach (Button btn in controlButtons)
            {
                resizeText(btn);
            }
            resizeText(label1);
        }

        private void resizeText(Control ctrl)
        {
            if (ctrl == null)
            {
                return;
            }
            float fontSize = ctrl.Font.Size;
            SizeF textSize;
            if (ctrl is Label)
            {
                ((Label)ctrl).UseCompatibleTextRendering = true;
            }
            Graphics gr = ctrl.CreateGraphics();
            textSize = gr.MeasureString(ctrl.Text, ctrl.Font);
            if (textSize.Width == 0)
            {
                return;
            }
            fontSize = fontSize * ctrl.ClientRectangle.Width / textSize.Width;
            ctrl.Font = new Font(ctrl.Font.FontFamily, fontSize);

            textSize = gr.MeasureString(ctrl.Text, ctrl.Font);
            if (textSize.Height > ctrl.ClientRectangle.Height)
            {
                fontSize = fontSize * ctrl.ClientRectangle.Height / textSize.Height;
                ctrl.Font = new Font(ctrl.Font.FontFamily, fontSize);
            }
            gr.Dispose();
            ctrl.Refresh();
        }

        private void InformationDialog_Shown(object sender, EventArgs e)
        {
            setComponents();
        }

        public static void Show(String labelText)
        {
            InformationDialog dlg = new InformationDialog(labelText);
            dlg.ShowDialog();
        }

        public static DialogResult Show(String labelText, MessageBoxButtons buttons)
        {
            InformationDialog dlg = new InformationDialog(labelText, buttons);
            dlg.ShowDialog();
            switch (dlg.DialogResult)
            {
                case DialogResult.Yes:
                    return DialogResult.Yes;
                default:
                    return DialogResult.No;
            }
        }
    }
}
